'''
Created on Mar 30, 2013

@author: yongchao
'''
import numpy as np
import matplotlib.pyplot as plt

class Network(object):
    
    def __init__(self):
        self.weightArrays=[]
        self.weightArray=[]
        
    def getWeightArray(self):
        return self.weightArray
    
    def getRandomWeightArray(self,size1):
        weightArray =  np.random.rand(1, size1)
        
        return weightArray

    def derivateActivation(self,inputValue):
        return inputValue
    
    def activateFun(self,inputValue):
        if inputValue > 0.5:
            returnValue = 1
        else:
            returnValue = 0
            
        return returnValue
        
    def activateFunSigmod(self,inputValue):
    
        returnValue = 1/(1+np.exp(-inputValue))
            
        return returnValue        
    
    def activateFunSigmodDerivative(self,inputValue):
    
        returnValue = 1/(1+np.exp(-inputValue))
        returnValue = returnValue * (1 - returnValue)
            
        return returnValue     
        
    def getLearningRate(self):
    
        return 0.1        
        
    def setWeigthArray(self,weightArr):
        self.weightArray = weightArr
        
    def addWeigthArrays(self,weightArr):
        self.weightArrays.append(weightArr)
#         print "add weight array in to weightArrays:"
#         print weightArr
            
    def getLastWeigthArray(self):
        lastIndex = len(self.weightArrays)
#         print "wright Arrays length is:"
#         print lastIndex
        lastWeightArr  = self.weightArrays[lastIndex-1]
        return  lastWeightArr        
        
    def printWeightData(self):
#         print "the optimized weight is:" 
        print self.weightArray
        
    def setBackPropLearingWeight(self,inputLayerWeight1,hiddenWeight1,inputLayerWeightAll1,hiddenWeightAll1,inputLayerWeightRepeat1,hiddenWeightRepeat1):
        self.lastInputLayerWeight = inputLayerWeight1
        self.lastHiddenWeight = hiddenWeight1
        
        self.inputLayerWeightAll = inputLayerWeightAll1
        self.hiddenWeightAll = hiddenWeightAll1
         
        self.inputLayerWeightRepeat = inputLayerWeightRepeat1
        self.hiddenWeightRepeat = hiddenWeightRepeat1   
                      
    def getInputLayerWeightAll(self):
        return self.inputLayerWeightAll    
    
    def getHiddenWeightAll(self):
        return self.hiddenWeightAll 
        
    def getInputLayerWeightRepeat(self):
        return self.inputLayerWeightRepeat    
    
    def getHiddenWeightRepeat(self):
        return self.hiddenWeightRepeat   
     
    def calcSquareDistance(self,input1,input2):
        #mean square distance y = 
        
        #calculate the mean square distance of two matrixes
        subWeightArr = input1-input2
        #    print "sub weight array is  :",subWeightArr
        subweightarray = subWeightArr.reshape((1,-1))

        multiValue = subweightarray * subweightarray.reshape((-1,1))
        #print "multiValue is       :",multiValue
        sumWeightVariation = np.sqrt( multiValue )
        resultValue = sumWeightVariation[0,0]
        #print "sum value is        :",sumWeightVariation   
        return   resultValue  
    
    def drawBackPropSquareDistance(self,inputLayerWeight,YValues,XLable,YLable,title):
    
        index  = 0
        for dotValue in range(0,inputLayerWeight[0]):
            if index == 0:
                break
            
            preValue =   dotValue
    
    
    
    
        plt.figure()
        plt.plot(XValues,YValues,label="$f(t)=e^{-t} \cdot \cos (2 \pi t)$")

        plt.axis()
        plt.xlabel(XLable)
        plt.ylabel(YLable)
        plt.title(title)

        plt.grid(True)
        plt.legend()
        plt.show()  

            
    def getLastInputLayerWeightArr(self):
        return self.lastInputLayerWeight    
    
    def getLastHiddenWeightArr(self):
        return self.lastHiddenWeight   
        
    def calcuSquareDistance(self,bottomValue,topValue,inputValue):
        #mean square distance y = 
        
        #calculate the mean square distance of two matrixes
        bottomDis = np.sqrt((bottomValue-inputValue)**2)
        topDis = np.sqrt((topValue-inputValue)**2)
        
        if bottomDis > topDis:
           return topValue
        else:
           return bottomValue
        
        


       
      